package com.potenza.training.dictionary.controller;

import com.potenza.training.dictionary.entity.ProjectTechnology;
import com.potenza.training.dictionary.entity.TechnologyOne;
import com.potenza.training.dictionary.server.ProjectTechnologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/hope")
public class ProjectTechnologyController {

    @Autowired
    private ProjectTechnologyService projectTechnologyService;

    static List<TechnologyOne> technologyOneList = new ArrayList<TechnologyOne>();

    /**
     * 获取所有的项目技术
     * @return
     */
    @GetMapping("/project/technology")
    public List<ProjectTechnology> findAll(){
        return projectTechnologyService.findAll();
    }

    /**
     * 获取经过处理的项目技术
     * @return
     */
    @GetMapping("/project/technologys")
    public List<TechnologyOne> edit(){
        technologyOneList.clear();
        List<ProjectTechnology> all = findAll();
        for (ProjectTechnology projectTechnology : all) {
            technologyOneList.add(new TechnologyOne(projectTechnology.getId(),projectTechnology.getName(),projectTechnology.getPid(),projectTechnology.getTypeId(),null));
        }

        List<TechnologyOne> collect = technologyOneList.stream().filter(technologyOne ->
                technologyOne.getPid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, technologyOneList));
            return menu;
        }).collect(Collectors.toList());

        return collect;
    }

    //递归查找所有菜单的子类菜单
    private static List<TechnologyOne> getChildrens(TechnologyOne root,List<TechnologyOne> all){
        List<TechnologyOne> children = all.stream().filter(linkageEntities -> Long.parseLong(String.valueOf(linkageEntities.getPid()))==root.getId()).map(linkageEntities -> {
            //1、递归找到子菜单
            linkageEntities.setChildren(getChildrens(linkageEntities,all));
            return linkageEntities;
        }).collect(Collectors.toList());
        return children;
    }
}
